package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.longjingscore.LongjingScoreRecord;
import com.ucredit.babel.common.model.third.longjingscore.LongjingScoreData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 龙井分
 *
 * @author xj
 */
public class LongjingScoreParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 龙井分
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        LongjingScoreRecord obj = (LongjingScoreRecord) model;
        LongjingScoreData scoreData = obj.getData();
        JSONObject info = JSON.parseObject(scoreData.getData());
        JSONObject data = info.getJSONObject("data");
        StringBuilder sb = getBaseInfo(obj, successful);
        //状态码
        sb.append(info.getString("code")).append(CRT001);
        //订单号
        sb.append(info.getString("orderId")).append(CRT001);
        //分值
        sb.append(data.getString("score")).append(CRT001);
        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_longjing_score_inc";
    }

    @Override
    public String getType() {
        return RecordType.LONGJING_SCORE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "龙井分";
    }


}
